<?php
/**
 * Created by PhpStorm.
 * User: ����
 * Date: 2015/11/12
 * Time: 16:17
 */

namespace app\controllers;

use app\models\GkyWeAccount;
use app\models\GkyWeArticle;
use app\models\GkyWeArticleType;
use app\models\GkyWeFans;
use app\models\GkyWeReplyNews;
use app\models\GkyWeReplyText;
use app\models\GkyWeWelcome;
use yii\web\Controller;
use Overtrue\Wechat\Server;
use Overtrue\Wechat\Message;
use Yii;
use app\common\utils\Wechat;
use yii\helpers\ArrayHelper;

define("TOKEN", "gky");
class WeixinController extends Controller
{
    public $layout='main_1';

    public function actionIndex($id){

        //获取当前公众号
        $weaccount = GkyWeAccount::findOne(['id'=>$id]);

        $server = new Server($weaccount->weappid, TOKEN, $weaccount->weadskey);

        //监听消息事件
        $server->on('message', function($message){

            if($message['MsgType'] == 'text'){
                $GLOBALS['replNews'] = GkyWeReplyNews::findByKey($message['ToUserName'],$message['Content']);
                if($GLOBALS['replNews']){
                    return Message::make('news')->items(function(){
                        return array(
                            Message::make('news_item')
                                ->title($GLOBALS['replNews']->title)
                                ->description($GLOBALS['replNews']->description)
                                ->picUrl($GLOBALS['replNews']->pic_url)
                                ->url($GLOBALS['replNews']->jump_url),
                        );
                    });
                }else{
                    $replyText = GkyWeReplyText::findByKey($message['ToUserName'],$message['Content']);
                    return Message::make('text')->content($replyText);
                }

            }else {
                return Message::make('text')->content('对不起，我们暂时无法处理您的消息');
            }

        });

        //监听关注事件
        $server->on('event', 'subscribe', function($event){

            $GLOBALS['welcome'] = GkyWeWelcome::find()
                ->where(['weoid'=>$event['ToUserName'],'isSet'=>1])
                ->one();

            return Message::make('news')->items(function(){
                return array(
                    Message::make('news_item')
                        ->title($GLOBALS['welcome']->title)
                        ->description($GLOBALS['welcome']->description)
                        ->picUrl($GLOBALS['welcome']->picUrl)
                        ->url($GLOBALS['welcome']->jumpUrl),
                );

            });

        });

        $result = $server->serve();

        echo $result;
    }

    /**
     * 微信授权获取用户信息
     */
    public function actionOauth(){

        $options = array(
            'token'=>'gky', //填写你设定的key
            'appid'=>'wxc3012b65e79bf64a', //填写高级调用功能的app id, 请在微信开发模式后台查询
            'appsecret'=>'7f7fee283610d3e3ec9dcb025b818518' //填写高级调用功能的密钥
        );
        $weObj = new Wechat($options); //创建实例对象
        $accessToken = $weObj->getOauthAccessToken();
        $userInfo = $weObj->getOauthUserinfo($accessToken['access_token'],$accessToken['openid']);
        $user = GkyWeFans::saveWithWeixin($userInfo);

        $this->redirect('http://gky.fukangyun.com/GkyWechat/gongzhonghao/gmain.html?userInfo='
            .json_encode(ArrayHelper::toArray($user)));
    }

}